package com.wyzz.hr.report.engine;

import com.wyzz.hr.base.FileOperate;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.SysConfigManager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

import java.io.File;
import java.util.HashMap;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.report.engine.api.*;
import org.eclipse.birt.report.model.api.*;

public class BirtEngineFactory {

	private BirtEngineFactory() {
	}

	public static synchronized IDesignEngine getBirtDesignEngine(
			ServletContext sc) {
		if (birtDesignEngine == null) {
			DesignConfig config = new DesignConfig();
			config.setPlatformContext(new PlatformServletContext(sc));
			config.setProperty("BIRT_HOME", "");
			birtDesignEngine = new DesignEngine(config);
			String reportDir = (new StringBuilder())
					.append(sc.getRealPath("/")).append(
							getPropertyFromConfigFile("report.dir")).toString();
			File rF = new File(reportDir);
			if (!rF.exists())
				rF.mkdirs();
			reportDir = (new StringBuilder()).append(sc.getRealPath("/"))
					.append(getPropertyFromConfigFile("report.custpredefine"))
					.toString();
			rF = new File(reportDir);
			if (!rF.exists())
				rF.mkdirs();
			reportDir = (new StringBuilder()).append(sc.getRealPath("/"))
					.append(getPropertyFromConfigFile("report.syspredefine"))
					.toString();
			rF = new File(reportDir);
			if (!rF.exists())
				rF.mkdirs();
		}
		return birtDesignEngine;
	}

	public static synchronized IReportEngine getBirtReportEngine(
			ServletContext sc) {
		if (birtReportEngine == null) {
			EngineConfig config = new EngineConfig();
			HashMap hm = config.getAppContext();
			hm.put("ctx", SpringBeanFactory.getSpringContext());
			hm.put("MyTools", new MyTools());
			hm.put("PropertiesReader", PropertiesFileConfigManager
					.getInstance());
			setLogLevel(config);
			config.setEngineHome("");
			org.eclipse.birt.core.framework.IPlatformContext context = new PlatformServletContext(
					sc);
			config.setPlatformContext(context);
			HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
			emitterConfig.setActionHandler(new HTMLActionHandler());
			HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
			emitterConfig.setImageHandler(imageHandler);
			config.getEmitterConfigs().put("html", emitterConfig);
			try {
				Platform.startup(config);
				IReportEngineFactory factory = (IReportEngineFactory) Platform
						.createFactoryObject("org.eclipse.birt.report.engine.ReportEngineFactory");
				birtReportEngine = factory.createReportEngine(config);
			} catch (BirtException e) {
				e.printStackTrace();
			}
		}
		return birtReportEngine;
	}

	public static synchronized void destroyBirtReportEngine() {
		if (birtReportEngine == null) {
			return;
		} else {
			birtReportEngine.destroy();
			Platform.shutdown();
			birtReportEngine = null;
			return;
		}
	}

	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	private static void setLogLevel(EngineConfig config) {
		String logLevel = getPropertyFromConfigFile("logLevel");
		if (logLevel != null || logLevel.trim().length() > 0) {
			Level level = Level.OFF;
			if ("SEVERE".equalsIgnoreCase(logLevel))
				level = Level.SEVERE;
			else if ("WARNING".equalsIgnoreCase(logLevel))
				level = Level.WARNING;
			else if ("INFO".equalsIgnoreCase(logLevel))
				level = Level.INFO;
			else if ("CONFIG".equalsIgnoreCase(logLevel))
				level = Level.CONFIG;
			else if ("FINE".equalsIgnoreCase(logLevel))
				level = Level.FINE;
			else if ("FINER".equalsIgnoreCase(logLevel))
				level = Level.FINER;
			else if ("FINEST".equalsIgnoreCase(logLevel))
				level = Level.FINEST;
			else if ("OFF".equalsIgnoreCase(logLevel))
				level = Level.OFF;
			String logDir = (new StringBuilder()).append(
					FileOperate.getFileHomePath()).append(
					getPropertyFromConfigFile("logDirectory")).toString();
			File logFile = new File(logDir);
			if (!logFile.exists())
				logFile.mkdirs();
			config.setLogConfig(logDir, level);
		}
	}

	private static String getPropertyFromConfigFile(String key) {
		SysConfigManager manager = PropertiesFileConfigManager.getInstance();
		return manager.getProperty(key);
	}

	private static IReportEngine birtReportEngine = null;
	private static IDesignEngine birtDesignEngine = null;

}
